package engine.control
{
	import engine.control.loading.NxLoading;
	import engine.control.loading.NxLoadingSkin;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.IEventDispatcher;
	
	import newx.BaseObject;
	import newx.StageInfo;
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.NxToolTips;
	import newx.com.control.Scroll.NxScrollPanel;
	import newx.com.control.button.NxButton;
	import newx.com.control.comboBox.NxComboBox;
	import newx.com.control.group.NxViewGroup;
	import newx.com.control.list.NxList;
	import newx.com.control.radio.NxRadio;
	import newx.com.control.tab.NxTab;
	import newx.com.control.table.NxTable;
	import newx.com.control.text.NxInput;
	import newx.com.control.text.NxLabel;
	import newx.com.control.text.NxText;
	import newx.com.control.window.NxWindow;
	import newx.com.control.window.NxWindowDragType;
	
	
	public class ControlCreater extends BaseObject
	{
		private static var _self:ControlCreater;
		
		private 	var timeAll		:uint	=	0;
		private	var creatCount	:int	=	0;
		private 	var typeobj		:Object	=	new Object();
		
		private	var	_controlConfig : ControlConfig;
		
		public function ControlCreater(target:IEventDispatcher=null)
		{
			super(target);
			
			_controlConfig = new ControlConfig();
		}
		
		public static function getInstance():ControlCreater
		{
			if(_self == null)
			{
				_self	=	new ControlCreater();
			}
			return _self;
		}
		
		public static function initHelp():void
		{
			MenuList.Init(StageInfo.uiSpace,"CustomControl_088","Button_38");
			NxToolTips.Init(StageInfo.systemSpace,uint(StaticIni.getIniVar("GameConst","tipMaxWidth")));
		}
		
		public function CreateControl(type:String,pList:Array,returnObj:Object,returnVarName:String):void
		{
			var tData	:Date;
			tData				=	new Date();
			var tTimes	:uint	=	0;
			
			switch(type)
			{
				case ControlType.NxWindow:
					returnObj[returnVarName]	=	CreateNxWindow(pList[0],uint(pList[1]),
																	uint(pList[2]),pList[3],pList[4]);
					break;
				case ControlType.NxButton:			
					returnObj[returnVarName]	=	CreateNxButton(pList[0],uint(pList[1]),
						uint(pList[2]),pList[3],int(pList[4]));			
					break;
//				case ControlType.ImageBox:
//					returnObj[returnVarName]	=	CreateImageBox(uint(pList[0]),uint(pList[1]),
//																pList[2]);
//					break;
				case ControlType.NxClip:
					returnObj[returnVarName]	=	CreateNxClip(Boolean(int(pList[0])),pList[1]);
					break;
//				case ControlType.List:
//					returnObj[returnVarName]	=	CreateList(uint(pList[0]),uint(pList[1]),
//																uint(pList[2]),pList[3],
//																pList[4],pList[5]);
//					break;
//				case ControlType.ButtonList:
//					var		btnArr:Array	=	new Array();
//					if(null != pList[0])
//					{
//						for(var i:int = 0; i < pList[0].length; i++)
//						{
//							btnArr.push(returnObj[pList[0][i]]);
//						}
//					}				
//					returnObj[returnVarName]	=	CreateButtonList(btnArr,
//																Boolean(int(pList[1])),
//																Boolean(int(pList[2])));
//					break;
				case ControlType.NxComboBox:	
					returnObj[returnVarName]	=	CreateNxComboBox();
					break;
//				case ControlType.TableEx2:
//					returnObj[returnVarName]	=	CreateTableEx2(uint(pList[0]),uint(pList[1]),
//						pList[2],pList[3],
//						pList[4]);
//					break;
				
				case ControlType.NxTable:
					returnObj[returnVarName]	=	CreateNxTable(
						uint(pList[0]),
						uint(pList[1]),
						pList[2] as Array,
						pList[3] as Array,
						pList[4],
						pList[5] as Array,
						pList[6],
						pList[7],
						uint(pList[8]),
						int(pList[9]),
						int(pList[10]));		
					break;					
				
				case ControlType.ScrollPanelEx:
					returnObj[returnVarName]	=	CreateScrollPanelEx(uint(pList[0]),uint(pList[1]),
																pList[2]);
					break;					
				case ControlType.CustomControl:
					returnObj[returnVarName]	=	CreateCustomControl(pList[0]);
					break;
				case ControlType.NxText:
					returnObj[returnVarName]	=	CreateNxText(uint(pList[0]),uint(pList[1]),pList[2],pList[3]);
					break;
				case ControlType.NxLabel:					
					returnObj[returnVarName]	=	CreateNxLabel();									
					break;
				case ControlType.NxInput:
					returnObj[returnVarName]	=	CreateNxInput(uint(pList[0]),uint(pList[1]),
																	String(pList[2]),uint(pList[3]),
																	uint(pList[4]),uint(pList[5]))
					break;
				case ControlType.NxViewGroup:					
					returnObj[returnVarName]	=	CreateNxViewGroup(uint(pList[0]),uint(pList[1]),String(pList[2]));
					break;
				case ControlType.LoadingEx:
					returnObj[returnVarName]	=	CreateLoadingBar(uint(pList[0]),uint(pList[1]),pList[2],Boolean(int(pList[3])));
					break;

//				//相册组件
//				case ControlType.Album:
//					returnObj[returnVarName]	=	CreateAlbum(uint(pList[0]),uint(pList[1]),
//																uint(pList[2]),uint(pList[3]),
//																uint(pList[4]),uint(pList[5]))
//					break;
				
				case ControlType.NxTab:
					returnObj[returnVarName]	=	CreateNxTab(uint(pList[0]),uint(pList[1]),
																Number(pList[2]),String(pList[3]))
					break;
				case ControlType.Sprite:
					returnObj[returnVarName]	=	CreateSprite()
					break;
				case ControlType.NxList:
					returnObj[returnVarName]	=	CreateNxList(pList[0],pList[1]);
					break;	
				case ControlType.NxRadio:
					returnObj[returnVarName]	=	CreateNxRadio(pList[0]);
					break;	
			}
			
//			tTimes	=	new Date().getTime() - tData.getTime();
//			timeAll	+= tTimes;
//			creatCount++
//			if(tTimes > 0)
//			{
//				trace(returnObj,returnVarName,type,"CreateControlTime:",tTimes);
//			}
//			if(type == "")
//			{
//				trace(returnObj,returnVarName,type)
//			}
//			
//			if(!typeobj.hasOwnProperty(type))
//			{
//				typeobj[type]	=	new Array();
//			}
//			typeobj[type].push(tTimes);
//			if(creatCount == 1101)
//			{
//				for(var tp:String in typeobj)
//				{
//					var tSize:int	=	(typeobj[tp] as Array).length;
//					var tUsedTimes:int	=	0;
//					for(var i:int = 0 ; i < tSize ; i ++)
//					{
//						tUsedTimes+=typeobj[tp][i];
//					}
//					typeobj[tp]		=	"cpuTime:"+int(tUsedTimes/tSize)+",count:"+tSize+"("+(tSize*100/1100).toFixed(2)+"%)";
//				}
//				trace("creatCount:",creatCount,"timeAll:"+timeAll);
//			}
//			
//			trace("creatCount:",creatCount);
		}
		/**
		 * 创建按钮 
		 * @param label
		 * @param w
		 * @param h
		 * @param skin
		 * @return 
		 * 
		 */		
		public function CreateNxButton(title:String="",w:uint=50,h:uint=20,_skinClassName:String="",topP:int = 0):NxButton
		{
			var tBtn:NxButton	=	new NxButton();
			tBtn.labelColor = _controlConfig.buttonLabelColor;
			tBtn.label		=	title;
			tBtn.skinWidth	=	w;
			tBtn.skinHeight	=	h;
			tBtn.skin		=	_skinClassName;
			tBtn.offsizeY	=	topP;
			
			return	tBtn;
		}
		
//		/**
//		 * 创建图片框 
//		 * @param _w
//		 * @param _h
//		 * @param _skinClassName
//		 * @param _imageUrl
//		 * @return 
//		 * 
//		 */		
//		public function CreateImageBox(_w:uint,_h:uint,_skinClassName:String = "",_imageUrl:Object = null):ImageBox
//		{
//			try
//			{
//				var img:ImageBox;
//				if(_skinClassName == "")
//				{
//					img	=	new ImageBox(_imageUrl,_w,_h,null);
//				}
//				else
//				{
//					img	=	new ImageBox(_imageUrl,_w,_h,ResManager.newResClass(_skinClassName) as MovieClip);
//				}
//			}
//			catch(e:Error)
//			{
//				img	=	null;
//				throw e;				
//			}			
//			return img;
//		}		
		
//		public function CreateList(_w:uint,_h:uint,_itemHeight:uint,_listArray:Array = null,_skinBackName:String="",_skinBtnName:String=""):ListEx
//		{
//			var tempListEx:ListEx
//			tempListEx	=	new ListEx(_w,_h,_itemHeight,_listArray,_skinBackName,_skinBtnName);
//			return tempListEx;
//		}		

		
//		public function CreateButtonList(cButtonArr:Array=null,_RCBth:Boolean=true,_autoChildren:Boolean=true):ButtonList
//		{
//			try
//			{
//				var tempButtonList:ButtonList;
//				tempButtonList = new ButtonList(cButtonArr,_RCBth);
//				tempButtonList.autoChildren=_autoChildren;
//			}
//			catch(e:Error)
//			{
//				tempButtonList	=	null;
//				throw e;				
//			}
//			
//			return tempButtonList;
//		}		
		
		public function CreateNxComboBox():NxComboBox
		{
			var comboBox : NxComboBox = new NxComboBox();
			comboBox.defaultTextColor	=	_controlConfig.comboBoxTextColor;
			return comboBox;
		}
		
		/**
		 * 
		 * @param tableWidth
		 * @param tableHeight
		 * @param titleLabels
		 * @param defaultColumWidthArr
		 * @param titleSkin
		 * @param itemSkins
		 * @param vSkin
		 * @param hSkin
		 * @param pageLinesConut
		 * @param titleHeight
		 * @param itemHeight
		 * @return 
		 * 
		 */		
		public function CreateNxTable(
			tableWidth			:uint,
			tableHeight			:uint,
			titleLabels			:Array,
			defaultColumWidthArr:Array,
			titleSkin			:String,
			itemSkins			:Array,
			vSkin				:String,
			hSkin				:String,
			pageLinesConut		:uint	=	0,
			titleHeight			:Number	=	25,
			itemHeight			:Number	=	25):NxTable
		{			
			var tempTableEx:NxTable		= 	new NxTable();
				
			tempTableEx.titleLabelColor = _controlConfig.tableLabelColor;
			tempTableEx.defaultTxtColor = _controlConfig.tableTxtColor;
			
			tempTableEx.width=tableWidth;
			tempTableEx.height=tableHeight;
			tempTableEx.titleLabels=titleLabels;
			tempTableEx.columWidthArr=defaultColumWidthArr;
//			tempTableEx.skin=titleSkin+","+itemSkins[0]+","+itemSkins[1]+","+vSkin+","+hSkin;
			
			tempTableEx.titleSkin	=	titleSkin;
			tempTableEx.contentSkin	=	itemSkins;
			tempTableEx.pageLinesConut=pageLinesConut;
			tempTableEx.titleHeight=titleHeight;
			tempTableEx.itemHeight=itemHeight;
			
			return tempTableEx;			
		}
		
//		public function CreateTableEx2(tWeidth:uint,minRow:uint,rowTitleLables:Array = null,titleSkin:String = null,itemSkins:Array = null):TableEx2
//		{
//			var tempTableEx:TableEx2;
//			tempTableEx		= new TableEx2(tWeidth,minRow,rowTitleLables,titleSkin,itemSkins);
//			return tempTableEx;
//		}
		
		public function CreateScrollPanelEx(width:uint,height:uint,_skin:Array = null):NxScrollPanel//view:DisplayObjectContainer,
		{
			var tempScrollPane:NxScrollPanel	=	new NxScrollPanel();
			
			tempScrollPane.viewWidth	=	width;
			tempScrollPane.viewHeight	=	height;
			tempScrollPane.width 		=	width;
			tempScrollPane.height		=	height;
			tempScrollPane.vScrollSkin	=	_skin[0];
			tempScrollPane.hScrollSkin	=	_skin[1];
			
			return tempScrollPane;
		}
		
		public function CreateLoadingBar(_w:uint,_h:uint,_skin:String,isPt:Boolean=false):NxLoading
		{
			return new NxLoading(_w,_h,new NxLoadingSkin(_skin,isPt));
		}
			
		/**
		 * 创建自定义组件 
		 * @param _skinClassName
		 * @return 
		 */		
		public function CreateCustomControl(_skinClassName:String	=	""):MovieClip
		{
			var mc : MovieClip;
			if(_skinClassName!=""&&_skinClassName!=null)
			{
				mc = ResManager.newResClass(_skinClassName) as MovieClip;
			}else{
				mc =  new MovieClip();
			}
			mc.stop();
			
			return mc;
		}
		
		//新加======================================================================================
		
		/**
		 * 文本组件
		 * @return 
		 * 
		 */		
		public function CreateNxText(width:uint,height:uint,vScrollSkin:String,hScrollSkin:String):NxText
		{
			var tTxt:NxText	=	new NxText();
			
			tTxt.width			=	width;
			tTxt.height			=	height;
			tTxt.vScrollSkin	=	vScrollSkin;
			tTxt.hScrollSkin	=	hScrollSkin;
			
			tTxt.color = _controlConfig.txtColor;
			
			return tTxt;
		}
		
		/**
		 * 简单文本组件，用于显示
		 * @return 
		 * 
		 */		
		public function CreateNxLabel():NxLabel
		{
			var label : NxLabel = new NxLabel();
			label.color = _controlConfig.labelColor;
			return label;
		}
		
//		/**
//		 * 相册组件
//		 * @return 
//		 * 
//		 */		
//		public function CreateAlbum(x:int,y:int,width:int,height:int,distance:int,count:int):Album
//		{
//			return new Album(x,y,width,height,distance,count);
//		}
		
		/**
		 * 按钮组组件
		 * @return 
		 * 
		 */		
		public function CreateNxTab(itemWidth:uint=80,itemHeight:uint=25,itemSpace:Number=0,
									itemSkin:String="",itemList:Array=null,
									viewX:int=10,viewY:int=56,viewW:Number=150,viewH:Number=100):NxTab
		{
			var tab : NxTab = new NxTab();
			tab.defaultLabelColor = _controlConfig.tabUnSeletedColor;
			tab.seletedLabelColor = _controlConfig.tabSeletedColor;
			tab.defaultHeight = itemHeight;
			tab.defaultWidth = itemWidth;
			tab.skin = itemSkin;
			return tab;
		}
	
		/**
		 * 空组件
		 * @return sprite
		 * 
		 */
		public function CreateSprite():Sprite
		{
			return new Sprite();
		}
		
		/**
		 *创建NxList2组件 
		 * 
		 * 
		*/
		public function CreateNxList(backgroundSkin:Array,data:Array):NxList
		{
			
			var temp:NxList=new NxList();
			temp.itemSkin = backgroundSkin[0];
			temp.vScrollSkin = backgroundSkin[1];
			temp.hScrollSkin = backgroundSkin[2];
	//		temp.skin=backgroundSkin[0]+","+backgroundSkin[1]+","+backgroundSkin[2];//得先设置皮肤数组，不然下面的设置会报错，因为皮肤数组默认为null,1
			if(data==null)
			{
				temp.dataProvider=[];
			}
			else
			{
				temp.dataProvider=data;
			}
//			temp.width=w;
//			temp.dataProvider=itemData;//2
//			temp.showCount=showCount;
//			temp.iconOffSizeX=leftOffSize;
//			temp.iconOffSizeY=verticalOffSize;
//			temp.itemAlign=alginVal;
			
			temp.defaultTxtColor = parseInt(StaticIni.getIniVar("Css_control_nxList", "itemColor"));
			
			return temp;
		}
		/**
		 * 创建单选组件 
		 * @param skinName
		 * @return 
		 * 
		 */		
		public function CreateNxRadio(skinName:String):NxRadio
		{
			var tempRadio:NxRadio	=	new NxRadio(skinName);
			return tempRadio;
		}
		
		/**
		 * 输入文本 
		 * @return 
		 * 
		 */
		public function CreateNxInput(w:uint,h:uint,_skin:String=null, l:uint=0,t:uint=0,c:uint= 0):NxInput
		{
			var input:NxInput=new NxInput();
			input.skin=_skin;
			input.skinWidth=w;
			input.skinHeight=h;
			input.width = w;
			input.height = h;
			input.offsizeX=l;
			input.offsizeY=t;
			
			if(c == 0)
			{			
				input.color = _controlConfig.inputColor;
			}
			else
			{
				input.color=c;
			}
			
			
			return input;
		}
		
		public function CreateNxClip(isCache:Boolean = false,defaultImageClass:String = null):NxClip
		{
			var defaultImage:DisplayObject	=	null;
			if(defaultImageClass != null)
			{
				defaultImage	=	ResManager.newResClass(defaultImageClass);
			}
			
			var tImg:NxClip		=	new NxClip();
			
			tImg.isCache		=	isCache;
			tImg.defaultImage	=	defaultImage;
			
			return tImg;
		}

		/**
		 * 创建基本窗口 
		 * @param title
		 * @param w
		 * @param h
		 * @param _skinClassName
		 * @param backType
		 * @return 
		 * 
		 */		
		public function CreateNxWindow(title:String = "",w:uint = 50,h:uint = 20,_skinClassName:String = "",backType:String="DragType_Title"):NxWindow
		{
			var tbWin:NxWindow;
			
			tbWin	=	new NxWindow();
			
			
			tbWin.title			=	title;
			tbWin.width			=	w;
			tbWin.height		=	h;
			
			if(_skinClassName != "")
			{
				tbWin.skin			=	_skinClassName;
			}
			tbWin.dragSpaceType	=	backType;
			
			//设置颜色
			tbWin.color = _controlConfig.windowLabelColor;
			
			return tbWin;
		}
		
		public function CreateNxViewGroup(row:uint,col:uint,baseItemRes:String):NxViewGroup
		{
			var tGruop:NxViewGroup	=	new NxViewGroup();
			
			tGruop.row		=	row;
			tGruop.col		=	col;
			tGruop.viewSkin	=	baseItemRes;
			return tGruop;
		}
	}
}
